var fixture = {
    date        : '',
    leagueid    : 0,
    url         : '',
    group       : 0,
    id          : '',
    loadingData : false,
    init: function(options){
        if (typeof options.id != 'undefined')
            this.id = options.id;
        if (typeof options.date != 'undefined')
            this.date = options.date;
        if (typeof options.leagueid != 'undefined')
            this.leagueid = options.leagueid;
        if (typeof options.url != 'undefined')
            this.url = options.url;
        if (typeof options.group != 'undefined')
            this.group = options.group;
        setTimeout(function(){fixture.autoChange()}, 300);
    },
    autoChange: function(){
        var parts = window.location.href.split('#!');
        if(parts.length > 1)
        {
            var temp = parts[1].split('/');
            this.date = temp[4];
            this.leagueid = temp[6];
            this.group = temp[8];
            $( "#datepicker" ).datepicker( "setDate", this.date );
            this.__changeContent(this.url + parts[1]);
        }
    },
    __changeContent: function(url){
        var keyCache = url;
        var cachedContent = null;
        /* Get content by jcache */
        try 
        {
            cachedContent = $.jCache.getItem(keyCache);
        } catch (ex) {
            cachedContent = null;
        }
        
        if (cachedContent == null || typeof cachedContent == 'undefined')
        {
            $.ajax({
                type: 'get',
                url: url,
                dataType: 'json',
                async: false,
                beforeSend: function(){
                    fixture.loadingData = true;
                    $('#' + fixture.id).html('Đang tải dữ liệu. Vui lòng chờ trong giây lát...');
                }
            }).success(function(response){
                cachedContent = response;
                $.jCache.setItem(keyCache, response);
            }).complete(function(){
                fixture.loadingData = false;
            });
        }
        
        if (!cachedContent.error)
        {
            $('#' + this.id).html(cachedContent.html);
            window.location.href = '#!' + url.replace(this.url, '');
        }
        $('html,body').animate({
            scrollTop: $("#" + fixture.id).offset().top - 25
        },'slow');
    },
    changeLeague: function(lid, group)
    {
        if (typeof group == 'undefined')
            group = this.group;
        if (typeof lid == 'undefined')
            lid = this.leagueid;
        this.group = group;
        this.leagueid = lid;
        if (!this.loadingData){
            this.__changeContent(this.url + '/lich-thi-dau/chitiet/ngay/' + this.date + '/lid/' + lid + '/g/' + group);
            return true;
        }
        else
            return false;
    },
    changeDate: function(date)
    {
        this.date = date;
        if (!this.loadingData){
            this.__changeContent(this.url + '/lich-thi-dau/chitiet/ngay/' + date + '/lid/' + this.leagueid + '/g/' + this.group);
            return true;
        }
        else
            return false;
    }
};